<?php
require_once('TML_glob.inc');
require_once('_connect.inc');

require_once (INCPATH.'VFS_ContextFactory.inc');
require_once (INCPATH.'VFS_Base.inc');

require_once (INCPATH.'CompHelper.inc');
require_once (INCPATH.'Template.inc');

require_once(INCPATH.'Debug.inc');

//-----------------------------------------------------------------------------
define ('ENEBLE_DIR_VIEW', false);

//-----------------------------------------------------------------------------
$_SERVER['REQUEST_URI'] = $_SERVER['REAL_REQUEST_URI'];
$memory                 = array();

//---Создаем контекст исполнения запроса---------------------------------------
$FSContextFact = new VFS_ContextFactory();
$FSContext     = $FSContextFact->createcontext(SEARCH_FOR_WEB,
																							 array('user_id'  => 32,
																										 'includes' => array(INCPATH),
																										 'php_comp' => array(PHPCOMPPATH),
																										 'files'    => array(MASSFILESPATH)));

//---Создаем файловую систему--------------------------------------------------
$createparams = array('parent'      => NULL,
                      'context'     => &$FSContext,
                      'searchcond'  => SEARCH_FOR_WEB,
                      'mountparams' => array('mountpoint' => '/',
                                             'token'      => 'root'));

$FS = VFS_Base::createfs('VFS_FSmodule', $FSContext['includes'], $createparams);

//-----------------------------------------------------------------------------
switch($FS->find($_SERVER['REQUEST_URI'], $FSObject))
{
	case OK:
		if($FSObject->type == 'dir')
		{
			$url         = $_SERVER['REQUEST_URI'];
			$Urllatssym  = substr($url, -1, 1);
			$INDEX_FILES = array('index.html', 'index.php');

			if($Urllatssym != '/')
			{
				header(sprintf('location:%s/', $_SERVER['REQUEST_URI']));
				exit();
			};

			for($i=0, $count = count($INDEX_FILES); $i < $count; $i++)
			{
				$index_file = $INDEX_FILES[$i];
				$FSOpStatus = $FS->find($index_file, $FSObject);

				if($FSOpStatus == OK)
				{
					$_SERVER['REQUEST_URI'].= $index_file;
					break;
				};
			};

			if($FSOpStatus != OK)
			{
				//индексных файлов не смогли таки найти
				header('HTTP/1.1 403');
				exit();
			};
		};
		
		//---------------------------------------------------------------------------
		$FSContext['REQUEST_URI'] = $_SERVER['REQUEST_URI'];

		//---------------------------------------------------------------------------
		if(method_exists($FSObject, 'GetView'))
		{
			$FSContext['file_info'] = $FSObject->GetInfo();
			$view                   = $FSObject->GetView();

			$view->render();
		}
		elseif(method_exists($FSObject, 'ProduceFile'))
		{
			$FSObject->ProduceFile();
		}
		else
		{
			printf ('Объект "%s" не поддерживает отображение', get_class($FSObject));
		};
	break;
	
	case NOT_FOUND:
		header('HTTP/1.1 404');
		exit();
	break;

	case ACCESS_DENIED:
		header('HTTP/1.1 403');
		exit();
	break;

	default:
		header('HTTP/1.1 400');
		exit();
};
?>